package com.pig4cloud.pig.file.controller;


import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.file.service.FarmFileService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
//@PreAuthorize("@pms.hasPermission('iot_iotDevice_view')" ) //todo 对于文件上传先不单分权限 先不加
@RequestMapping("/farmFile" )
@Tag(description = "farmFile" , name = "农业模块通用文件管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class FarmFileController {

	private final FarmFileService farmFileService;


	/**
	 * 文件上传
	 * @param file
	 * @return
	 */
	@PostMapping(value = "/upload")
	public R uploadFile(@RequestPart("file") MultipartFile file) {
		return (R<Object>) farmFileService.uploadFarmFile(file);
	}


	/**
	 * 文件下载 预览
	 * @param bucket 桶名称
	 * @param fileName 文件空间/名称
	 * @param response
	 * @return
	 */
	@Inner(false) //服务调用不校权限注解
	@SneakyThrows
	@GetMapping("/{bucket}/{relativDir}/{year}/{month}/{day}/{fileName}")
	public void getToDetFile(@PathVariable String bucket,
							 @PathVariable String relativDir,
							 @PathVariable String year,
							 @PathVariable String month,
							 @PathVariable String day,
							 @PathVariable String fileName,
							 HttpServletResponse response) {
		String fileDir=String.format("%s/%s/%s/%s/%s",bucket, relativDir,year, month, day);
		farmFileService.getFile(fileDir, fileName, response);
	}

}
